package cn.njupt.unionfind;
//对快速合并的优化，不是每次都从左边嫁接到右边，而是从数量进行判断
public class Quick_Union_Size extends Quick_Union {
    int[] sizes;

    public Quick_Union_Size(int capacity) {
        super(capacity);
        sizes=new int[capacity];
        for (int i = 0; i <capacity ; i++) {
            sizes[i]=1;
        }
    }

    @Override
    public void union(int child1, int child2) {
        int p1=find(child1);
        int p2=find(child2);
        if(p1==p2)return;
        if(sizes[p1]<sizes[p2]){
            //p1嫁接到p2上
            parent[p1]=p2;
            sizes[p2]+=sizes[p1];
        }else {
            parent[p2]=p1;
            sizes[p1]+=sizes[p2];
        }
    }
}
